package com.sakura.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.common.CustomerException;
import com.sakura.dto.SetmealDto;
import com.sakura.mapper.SetmealMapper;
import com.sakura.pojo.Category;
import com.sakura.pojo.Setmeal;
import com.sakura.pojo.SetmealDish;
import com.sakura.serivce.CategoryService;
import com.sakura.serivce.SetmealDishService;
import com.sakura.serivce.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Override
    @Transactional
    public void saveWithSetMealAndDish(SetmealDto setmealDto) {
        // 保存到SetMeal中
        this.save(setmealDto);

        // 保存到SetMealDish中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page<SetmealDto> pageWithClassify(Integer page, Integer pageSize, String name) {
        // 添加查询条件
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        setmealQueryWrapper.orderByDesc("update_time");
        // 查询呢SetMeal中的分页数据
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        // 查询数据库
        this.page(setmealPage, setmealQueryWrapper);

        // 把Setmeal中的数据复制到DTO中
        Page<SetmealDto> setmealDtoPage = new Page<>();
        List<Setmeal> recordsSetmeals = setmealPage.getRecords();
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        // 把每一个SetMeal数据变成SetMealDTO数据，并查询Category表得到Name数据，赋值给SetMealDTO中的CategoryName
        for (Setmeal setmeal : recordsSetmeals) {
            // 用于储存records数据的单个数据
            SetmealDto setmealDto = new SetmealDto();
            // 复制setmeal到setmealDTO
            BeanUtils.copyProperties(setmeal, setmealDto);
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);

        }
        // 把List集合赋值到DTO中完成数据转移
        setmealDtoPage.setRecords(setmealDtoList);
        return setmealDtoPage;
    }

    @Override
    public SetmealDto selectSetmealDtoById(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        // 复制DTO父类中基本属性
        BeanUtils.copyProperties(setmeal, setmealDto);
        // 查询Name
        Category category = categoryService.getById(setmeal.getCategoryId());
        setmealDto.setCategoryName(category.getName());

        // 查询套餐关联菜品，关联表为SetMealDish表
        QueryWrapper<SetmealDish> setmealDishQueryWrapper = new QueryWrapper<>();
        setmealDishQueryWrapper.eq("setmeal_id", setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(setmealDishQueryWrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    public void removeWithDishByIds(List<Long> ids) {
        // 判断套餐状态是否启售
        // select * form setmeal where id in ids and status = 0
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        wrapper.eq("status", 1);
        int count = this.count(wrapper);
        if (count > 0) {
            throw new CustomerException("该套餐为启售状态");
        }

        // 根据数组删除套餐表
        QueryWrapper<Setmeal> removeSetMealWrapper = new QueryWrapper<>();
        removeSetMealWrapper.in("id",ids);
        // 删除SetMeal表
        this.remove(removeSetMealWrapper);
        // 删除SetMealDish表
        QueryWrapper<SetmealDish> removeDishWrapper = new QueryWrapper<>();
        removeDishWrapper.in("setmeal_id",ids);
        setmealDishService.remove(removeDishWrapper);
    }

    @Override
    public void updateStatusByIds(Integer status, Long[] ids) {
        // 添加修改条件
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.in("id",Arrays.asList(ids));
        // 创建临时变量用于修改状态
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        this.update(setmeal,setmealQueryWrapper);
    }
}
